package com.quanquan.management.controller;

import DTO.PageDTO;
import base.BaseConst;
import com.quanquan.management.dto.NoticeDTO;
import com.quanquan.management.request.NoticeRequestBody;
import com.quanquan.management.service.NoticeService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import web.response.ResultBody;

/**
 * @ClassName:NoticeController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/5/5 16:45
 * @Version 1.0
 */
@Controller
@RequestMapping("/notice")
public class NoticeController extends ManagerBaseController{

    private final NoticeService noticeService;

    @Autowired
    public NoticeController(NoticeService noticeService) {
        this.noticeService = noticeService;
    }

    @RequestMapping("/list")
    public String list(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String sort,
                       @RequestParam(required = false) String fromDate,
                       @RequestParam(required = false) String toDate,
                       @RequestParam(required = false) boolean isAsc,
                       @RequestParam(required = false) Integer sta,
                       Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        NoticeRequestBody requestBody = new NoticeRequestBody(page, pageSize, keyword, sort, isAsc);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        requestBody.setStatusShow(sta);
        PageDTO<NoticeDTO> pageDTO = noticeService.list(requestBody);
        model.addAttribute("pageObj", pageDTO);
        return "notice/list";
    }

    /**
     * 添加
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(NoticeRequestBody requestBody, Model model) {
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            return "notice/form";
        }
        boolean add = noticeService.addNotice(requestBody);
        if (add) {
            return "redirect:/notice/list";
        } else {
            return null;
        }
    }

    /**
     * 编辑
     * @param id
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(String id, NoticeRequestBody requestBody, Model model) {
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            NoticeDTO byId = noticeService.findById(id);
            model.addAttribute("editable",byId);
            return "notice/form";
        }
        boolean update = noticeService.update(requestBody);
        if (update) {
            return "redirect:/notice/list";
        } else {
            return null;
        }
    }

    /**
     * 添加
     * @param requestBody
     * @return
     */
    @RequestMapping("/delete")
    public String delete(NoticeRequestBody requestBody) {
        boolean add = noticeService.delete(requestBody);
        if (add) {
            return "redirect:/notice/list";
        } else {
            return null;
        }
    }


    @RequestMapping("/updateStatus")
    @ResponseBody
    public ResultBody updateStatus(@RequestParam String id, @RequestParam Integer status) {
        NoticeRequestBody requestBody = new NoticeRequestBody();
        requestBody.setId(id);
        requestBody.setStatusShow(status);
        Integer changeStatus = null;
        if (status == 1) {
            changeStatus = 0;
        }
        if (status == 0) {
            changeStatus = 1;
        }
        boolean b = noticeService.updateStatus(requestBody);
        if (b) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),changeStatus);
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(),ResCodeEnum.FAIL.getMsg());
    }




}
